package com.cgy.utils.base.jvm;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryUsage;
import java.lang.management.OperatingSystemMXBean;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * Jvm工具类
 * @author CH
 *
 */
public class JvmHelper {
	

	public static class System {
		
		private static OperatingSystemMXBean system = ManagementFactory.getOperatingSystemMXBean();
		/**
		 * 系统名称
		 * @return
		 */
		public static String name() {
			return system.getName();
		}
		/**
		 * 系统版本
		 * @return
		 */
		public static String version() {
			return system.getVersion();
		}
		/**
		 * 操作系统的架构
		 * @return
		 */
		public static String arch() {
			return system.getArch();
		}
		/**
		 * 可用的内核数
		 * @return
		 */
		public static int availableProcessors() {
			return system.getAvailableProcessors();
		}
	}
	
	public static class Vm {
		
		private static final long MB = 1024 * 1024;
		
		/**
		 * 内存信息
		 * @return
		 */
		public static List<Map<String, Object>> memoryPool() {
			List<Map<String, Object>> result = Lists.newArrayList();
			
			List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
			if(pools != null && !pools.isEmpty()){
				for(MemoryPoolMXBean pool : pools){
					Map<String, Object> one = Maps.newHashMap();
					one.put("name", pool.getName());
					one.put("total", pool.getUsage().getMax() / MB);
					one.put("used", pool.getUsage().getUsed() / MB);
					one.put("commit", pool.getUsage().getCommitted() / MB);
					one.put("use", pool.getUsage().getUsed()*100/pool.getUsage().getCommitted() + "%");
					
					result.add(one);
				}
			}
			
			return result;
		}
	}
	public static class Pid {

		/**
		 * PID
		 * @return
		 */
		public static String getPid() {
			String name = ManagementFactory.getRuntimeMXBean().getName();  
			return name.split("@")[0];  
		}
	}
	public static class Memory {

		private static final long MB = 1024 * 1024;
		/**
		 * 最大内存
		 * @return
		 */
		public static long total() {
			MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
			long max = memoryMXBean.getHeapMemoryUsage().getMax();
			return max;
		}
		
		/**
		 * 已使用内存
		 * @return
		 */
		public static long used() {
			MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
			long used = memoryMXBean.getHeapMemoryUsage().getUsed();
			return used;
		}

		/**
		 * 已使用内存
		 * @return
		 */
		public static String less() {
			MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
			long used = memoryMXBean.getHeapMemoryUsage().getUsed();
			long max = memoryMXBean.getHeapMemoryUsage().getMax();
			
			long usedM = used / MB;
			long maxM = max / MB;
			return "(" + usedM + "/" + maxM + ")M";
		}
		/**
		 * head堆 使用率
		 * @return
		 */
		public static String head() {
			MemoryMXBean memory = ManagementFactory.getMemoryMXBean();
			MemoryUsage headMemory = memory.getHeapMemoryUsage();
			return headMemory.getUsed() * 100 / headMemory.getCommitted() + "%";
		}
	}
}
